#include <sstream>
#include <iostream>

#include "c_SpriteResource.h"


wiigi::c_SpriteResource::c_SpriteResource( c_ResourceMgr* ResourceMgr )
:
   //Base
   c_Resource( ResourceMgr ),
   //members
   m_TextureName(),
   m_TextureName_Set( false ),
   m_SubRect(),
   m_SubRect_Set( false ),
   m_FrameCnt( 0 ),
   m_FrameCnt_Set( false ),
   m_Sprites()
{
   
   return;  
}

bool wiigi::c_SpriteResource::cache( void )
{
   
   return true;
}

bool wiigi::c_SpriteResource::purge( void )
{
   std::list<c_Sprite*>::iterator it;

   for( it = m_Sprites.begin(); it != m_Sprites.end(); ++it )
   {
      c_Sprite* Sprite;
      Sprite = *it;
      
      std::cerr << "c_SpriteResource::purge( " << (void*)Sprite << " )\n";

      delete Sprite;
   }
   m_Sprites.clear();

   return true;
}

bool wiigi::c_SpriteResource::setParameter( const std::string& ParameterName, const std::string& ParameterValue )
{
   printf("c_SpriteResource::setParameter: %s=%s\n", ParameterName.c_str(), ParameterValue.c_str() );
   
   bool Supported;
   Supported = true;
   
   if( ParameterName == "texture" )
   {
      setTextureName( ParameterValue );
   }
   else if( ParameterName == "subrect" )
   {
      setSubRect( ParameterValue );
   }
   else if( ParameterName == "frame_cnt" )
   {
      setFrameCnt( ParameterValue );
   }
   else
   {
      Supported = c_Resource::setParameter( ParameterName, ParameterValue );
   }
   
   return Supported;
}

void wiigi::c_SpriteResource::setSubRect( const std::string& SubRect )
{
   std::stringstream ss( SubRect, std::stringstream::in );
   
   ss >> m_SubRect.m_Left;
   ss.ignore( 1, ',' );
   ss >> m_SubRect.m_Top;
   ss.ignore( 1, ',' );
   ss >> m_SubRect.m_Right;
   ss.ignore( 1, ',' );
   ss >> m_SubRect.m_Bottom;

   //printf("Ret=(%u,%u,%u,%u)\n", m_SubRect.m_Left, m_SubRect.m_Top, m_SubRect.m_Right, m_SubRect.m_Bottom );
   
   m_SubRect_Set = true;
}


void wiigi::c_SpriteResource::setTextureName( const std::string& TextureName )
{
   m_TextureName = TextureName;
   
   m_TextureName_Set = true;
   
   return;
}

void wiigi::c_SpriteResource::setFrameCnt( const std::string& FrameCnt )
{
   std::stringstream ss( FrameCnt, std::stringstream::in );
   
   ss >> m_FrameCnt;
   
   printf("FrameCnt=%u\n", m_FrameCnt );
   
   m_FrameCnt_Set = true;
   
   return;
}

wiigi::c_Sprite* wiigi::c_SpriteResource::createSprite( void )
{
   printf("createSprite: %s\n", getName().c_str() );
   
   c_Sprite* Sprite;
   Sprite = 0;
   
   if( m_TextureName_Set )
   {
      c_Texture* Texture;
      Texture = m_ResourceMgr->getTexture( m_TextureName );
      if( 0 != Texture )
      {
         if( m_SubRect_Set )
         {
            if( m_FrameCnt_Set )
            {
               Sprite = m_Engine->createSprite( Texture, m_SubRect, m_FrameCnt );
            }
            else
            {
               Sprite = m_Engine->createSprite( Texture, m_SubRect );
            }
         }
         else
         {
            Sprite = m_Engine->createSprite( Texture );
         }
      }
   }
   
   if( 0 != Sprite )
   {
      m_Sprites.push_back( Sprite );
   }
   
   return Sprite;
}

bool wiigi::c_SpriteResource::freeSprite( c_Sprite* DeadSprite )
{
   std::list<c_Sprite*>::iterator it;

   for( it = m_Sprites.begin(); it != m_Sprites.end(); ++it )
   {
      c_Sprite* Sprite;
      Sprite = *it;
      if( Sprite == DeadSprite )
      {
         m_Sprites.erase( it );
         delete Sprite;
         return true;
      }
   }

   return false;
}
